<template>
  <div style="width:100%;overflow-x:hidden;overflow-y:auto">
    <weui-input :list="word_list" title="word" :show_more="true" :tips="null" agreement_name=""
                @on-more="word_list.push({})">
    </weui-input>
    <weui-input :list="url_list" title="url" :show_more="true" :tips="null" agreement_name=""
                @on-more="url_list.push({})">
    </weui-input>
    <weui-input :list="html_list" title="html" :show_more="true" :tips="null" agreement_name=""
                @on-more="html_list.push({type:'div', value:''})">
      <weui-button slot="button" type="primary" @on-click="onSubmitClick">
        submit
      </weui-button>
    </weui-input>
  </div>
</template>

<script>
  import WeuiInput from '../vue2componentSet/src/components/WeuiInput'
  import WeuiButton from '../vue2componentSet/src/components/WeuiButton'
  import global from '../lib/global'
  import axios from 'axios'

  export default {
    components: {
      WeuiInput,
      WeuiButton
    },
    data() {
      return {
        word_list: [],
        url_list: [],
        html_list: []
      }
    },
    methods: {
      onSubmitClick() {
        if (global.is_debug) {
          this.url_list = []
          this.word_list = []
          this.html_list = []
          return
        }
        var urls = [],
          words = [],
          htmls = []
        this.url_list.forEach(item => {
          if (/^http/.test(item.value))
            urls.push(item.value)
        })
        this.word_list.forEach(item => {
          if (item.value)
            words.push(item.value)
        })
        this.html_list.forEach(item => {
          if (item.value)
            htmls.push(item.value)
        })
        this.$store.commit('loading', {
          is_show: true,
          content: 'adding'
        })
        axios.post('/add.do', {
          urls: urls,
          words: words,
          htmls: htmls
        }).then(result => {
          const obj = result.data
          if (obj.result == 1) {
            this.url_list = []
            this.word_list = []
            this.html_list = []
          } else if (obj.result == 0) {
            this.$store.commit('login', '')
            this.$store.commit('toast', {
              type: 'warn',
              content: '请登录'
            })
            this.$router.push('/login?redirectTo=' + encodeURIComponent(location.hash.substring(1)))
          } else {
            this.$store.commit('toast', {
              type: 'warn',
              content: obj.msg
            })
          }
          this.$store.commit('loading', {
            is_show: false
          })
        }, (response) => {
          console.log(response)
          this.$store.commit('loading', {
            is_show: false
          })
        })
      }
    },
    mounted() {
      this.$store.commit('set_title', 'add url')
      if (!this.$store.state.username) {
        this.$router.redirect('/login?redirectTo=' + encodeURIComponent(location.hash.substring(1)))
        return
      }
    }
  }
</script>
